package com.zoke.loveway.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zoke.loveway.R;
import com.zoke.support.base.BaseActivity;
import com.zoke.support.base.BaseFragment;

/**
 * 封装项目的title 使用这个title处理时必须将app_title.xml布局include到你的主布局中
 * 支持Activity及Fragment中使用 ----可减低耦合性 利用父类来设置title的颜色或者背景色
 **/
public class TitleManager {

	protected BaseActivity mBase;
	protected BaseFragment mBaseFragment;

	protected Context mContext;

	public TitleManager(BaseActivity baseAct) {
		this.mBase = baseAct;
		mContext = mBase;
	}

	public TitleManager(BaseFragment baseFragment) {
		this.mBaseFragment = baseFragment;
		mContext = mBaseFragment.getActivity();
	}

	/** 获取左侧按钮引用 **/
	public Button getTitleLeft() {
		// 如果activity==null 则是fragment
		return mBase == null ? (Button) mBaseFragment
				.findViewById(R.id._title_left) : (Button) mBase
				.findViewById(R.id._title_left);
	}

	/** 获取左侧按钮的父容器 **/
	public RelativeLayout getTitleLeftContainer() {
		// 如果activity==null 则是fragment
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._title_left_container)
				: (RelativeLayout) mBase
						.findViewById(R.id._title_left_container);
	}

	/** 设置左侧按钮图标 **/
	public TitleManager setLeftIcon(int resid) {
		RelativeLayout titleBtnCon = getTitleLeftContainer();
		Button titleBtn = getTitleLeft();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnCon.setOnClickListener(mBaseFragment);
		// 设置图标的时候设置显示
		titleBtnCon.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	/** 隐藏按钮 **/
	public TitleManager hideLeft() {
		getTitleLeftContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/** 显示按钮 **/
	public TitleManager showLeft() {
		getTitleLeftContainer().setVisibility(View.VISIBLE);
		return this;
	}

	/** 获取title引用 **/
	public TextView getTitle() {
		return mBase == null ? (TextView) mBaseFragment
				.findViewById(R.id._title) : (TextView) mBase
				.findViewById(R.id._title);
	}

	/** 获取title的父容器 **/
	public RelativeLayout getTitleContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._title_container) : (RelativeLayout) mBase
				.findViewById(R.id._title_container);
	}

	private String getReString(int resId) {
		if (mContext == null)
			return "titleManager异常";
		return mContext.getResources().getString(resId);
	}

	/** 设置标题内容 **/
	public TitleManager setTitle(int resId) {
		return setTitle(getReString(resId));
	}

	/** 设置标题内容 **/
	public TitleManager setTitle(CharSequence text) {
		RelativeLayout titleBtnContainer = getTitleContainer();
		// 先显示在设置
		TextView titleBtn = getTitle();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setText(text);
		return this;
	}

	/** 设置标题的背景 **/
	public TitleManager setTitleIcon(int resid) {
		RelativeLayout titleBtnContainer = getTitleContainer();
		// 先显示再设置
		TextView titleBtn = getTitle();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	/** 隐藏title标题 **/
	public TitleManager hideTitle() {
		getTitleContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/** 显示title标题 **/
	public TitleManager showTitle() {
		getTitleContainer().setVisibility(View.VISIBLE);
		return this;
	}

	/** 获取右侧按钮引用 **/
	public Button getTitleRight() {
		return mBase == null ? (Button) mBaseFragment
				.findViewById(R.id._title_right) : (Button) mBase
				.findViewById(R.id._title_right);
	}

	/** 获取右侧按钮的父容器 **/
	public RelativeLayout getTitleRightContainer() {
		return mBase == null ? (RelativeLayout) mBaseFragment
				.findViewById(R.id._title_right_container)
				: (RelativeLayout) mBase
						.findViewById(R.id._title_right_container);
	}

	/** 设置右侧按钮图标 **/
	public TitleManager setRightIcon(int resid) {
		RelativeLayout titleBtnContainer = getTitleRightContainer();
		// 先显示再设置
		Button titleBtn = getTitleRight();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		titleBtn.setBackgroundResource(resid);
		return this;
	}

	/** 设置右侧文字 **/
	public TitleManager setRightText(int resId) {
		return setRightText(getReString(resId));
	}

	public TitleManager setRightText(CharSequence text) {
		RelativeLayout titleBtnContainer = getTitleRightContainer();
		// 先显示再设置
		Button titleBtn = getTitleRight();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		LayoutParams params = titleBtn.getLayoutParams();
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		titleBtn.setText(text);
		return this;
	}

	/** 设置左侧文字 **/
	public TitleManager setLeftText(int resId) {
		return setLeftText(getReString(resId));
	}

	/** 设置左侧按钮为文字 **/
	public TitleManager setLeftText(CharSequence text) {
		RelativeLayout titleBtnContainer = getTitleLeftContainer();
		// 先显示再设置
		Button titleBtn = getTitleLeft();
		// 如果是FragMent需要设置点击事件
		if (null != mBaseFragment)
			titleBtnContainer.setOnClickListener(mBaseFragment);
		titleBtnContainer.setVisibility(View.VISIBLE);
		LayoutParams params = titleBtn.getLayoutParams();
		params.width = LayoutParams.WRAP_CONTENT;
		params.height = LayoutParams.WRAP_CONTENT;
		titleBtn.setText(text);
		return this;
	}

	/** 隐藏右侧按钮 **/
	public TitleManager hideRight() {
		getTitleRightContainer().setVisibility(View.INVISIBLE);
		return this;
	}

	/** 显示右侧按钮 **/
	public TitleManager showRight() {
		getTitleRightContainer().setVisibility(View.VISIBLE);
		return this;
	}
}
